package org.bbl.web;

import com.google.inject.Inject;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.bbl.db.domain.Bbl;
import org.bbl.db.domain.Book;
import org.bbl.db.domain.Language;
import org.bbl.db.mybatis.mapper.BblMapper;
import org.bbl.db.mybatis.mapper.BookMapper;
import org.bbl.db.mybatis.mapper.LanguageMapper;
import org.bbl.db.mybatis.mysql.MyEntityResolver;
import org.bbl.web.panel.EntityEditor;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author biniam.gebremichael
 * @since 4/7/13
 */
public class ListPage extends Panel {


    @Inject
    private MyEntityResolver entityResolver;

    private Label getFontedLabel(String id, String value, Language language) {
        Label text = new Label(id, value);
        if (language.getFamilyId() == 2) {
            text.add(new AttributeModifier("class", "geez"));
        }
        return text;
    }

    public ListPage(final String id, final Integer bookId,
                    final Integer chapterId, final Integer languageId) {
        super(id);
        Book book = getBook(bookId, languageId);
        Language language = entityResolver.findById(LanguageMapper.class, languageId);
        add(getFontedLabel("bookName", book.getMidName(), language));
        add(new Label("chapterId", String.valueOf(chapterId)));

        add(getVerses(bookId, chapterId, language, book));
    }

    private Book getBook(int bookId, int languageId) {
        return entityResolver.findById(BookMapper.class, bookId, languageId);
    }

    private ListView<Bbl> getLinkVerses(final Language language, List<Bbl> linkedBbl) {
        return new ListView<Bbl>("link", linkedBbl) {
            @Override
            protected void populateItem(ListItem<Bbl> components) {
                final Bbl bbl = components.getModelObject();
                final Book book = getBook(bbl.getBookId(), language.getLanguageId());
                Link<Void> nav = new Link<Void>("linkNav") {
                    @Override
                    public void onClick() {
                        throw new RestartResponseAtInterceptPageException(
                                new HomePage(book.getBookId(), bbl.getChapterId(), language.getLanguageId(), 1));
                    }
                };
                nav.add(new Label("linkBookName", book.getShortName()));
                nav.add(new Label("linkNumber", bbl.getShortNumber()));
                components.add(nav);
                String verse = bbl.getVerse().length() > 28 ? bbl.getVerse().substring(0, 24) + " ..." : bbl.getVerse();
                Label linkVerse = getFontedLabel("linkVerse", verse, language);
                linkVerse.add(new AttributeModifier("title", bbl.getVerse()));
                components.add(linkVerse);
            }
        };
    }

    private ListView<Bbl> getVerses(Integer bookId, Integer chapterId, final Language language, final Book book) {
        List<Bbl> byKeyId = entityResolver.findByKeyId(BblMapper.class,
                language.getShortName(),
                bookId,
                chapterId);
        return new ListView<Bbl>("list", byKeyId) {
            @Override
            protected void populateItem(ListItem<Bbl> components) {
                Bbl bbl = components.getModelObject();
                components.add(new Label("number", String.valueOf(bbl.getStartVerse())));
                components.add(getFontedLabel("verse", bbl.getVerse(), language));
                final EntityEditor reportError = new EntityEditor("reportError", 480, 450, book, bbl);
                components.add(reportError);

                AjaxLink<Void> showEdit = new AjaxLink<Void>("showReportError") {
                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        reportError.show(target);
                    }
                };

                if (language.getLanguageId() != 2) {
                    showEdit.setVisible(false);

                }

                components.add(showEdit);
                final WebMarkupContainer container = new WebMarkupContainer("linkPanel");
                List<Bbl> linkedBbl = entityResolver.findLinking(BblMapper.class, language.getShortName(), bbl.getBookId(), bbl.getChapterId(), bbl.getStartVerse());

                ListView<Bbl> linkVerses = getLinkVerses(language, linkedBbl);
                container.add(linkVerses);
                container.setOutputMarkupPlaceholderTag(true).setOutputMarkupId(true).setVisible(false);
                components.add(container);

                AjaxFallbackLink<Void> showLink = getLink("showLink", container);
                components.add(showLink);
                if (linkedBbl.size() <= 0) {
                    showLink.setVisible(false);
                }
            }
        };
    }

    private AjaxFallbackLink<Void> getLink(final String identifier, final WebMarkupContainer container) {
        final Label icon = new Label(identifier + "Icon", "");
        icon.setOutputMarkupId(true);
        icon.add(new AttributeModifier("class", new Model<Serializable>() {
            @Override
            public Serializable getObject() {
                if (container.isVisible()) {
                    return "icon-minus";
                } else {
                    return "icon-plus";
                }
            }
        }));

        AjaxFallbackLink<Void> link = new AjaxFallbackLink<Void>(identifier) {
            @Override
            public void onClick(AjaxRequestTarget ajaxRequestTarget) {
                container.setVisible(!container.isVisible());
                ajaxRequestTarget.add(container);
                ajaxRequestTarget.add(icon);
            }
        };
        link.add(icon);
        return link;
    }
}
